<template>
  <div class="content">

    <el-tabs
        v-model="queryForm.status"
        type="card"
        @tab-change="handleStatus">

      <el-tab-pane label="全部" name="0"/>
      <el-tab-pane label="待接单" name="2"/>
      <el-tab-pane label="已接单" name="3"/>
      <el-tab-pane label="派送中" name="4"/>
      <el-tab-pane label="已完成" name="5"/>
      <el-tab-pane label="已取消" name="6"/>
    </el-tabs>

    <el-form :inline="true" :model="queryForm">
      <el-form-item label="编号">
        <el-input v-model="queryForm.num" placeholder="请输入编号" clearable />
      </el-form-item>
      <el-form-item label="手机">
        <el-input v-model="queryForm.phone" placeholder="请输入手机" clearable />
      </el-form-item>

      <el-form-item label="下单时间">
        <el-date-picker
            v-model="queryForm.valueTime"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期",
            format="YYYY-MM-DD"
            value-format="YYYY-MM-DD"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="loadBookList">查询</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="bookList" stripe>
      <el-table-column prop="num" label="编号" width="140">
        <template #default="scope">
          <span  @click="handleDetail(scope.row.num)">{{ scope.row.num }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="customerName" label="顾客" width="100" />
      <el-table-column prop="phone" label="手机" width="100" />

      <el-table-column prop="address" label="地址" width="140">
        <template #default="scope">
          <el-popover effect="light" trigger="hover" placement="top" width="auto">
            <template #default>
              <div>{{ scope.row.address }}</div>
            </template>
            <template #reference>
              <el-text  truncated>
                {{ scope.row.address }}
              </el-text>
            </template>
          </el-popover>
        </template>
      </el-table-column>

      <el-table-column prop="amount" label="金额" width="100"/>
<!--      <el-table-column prop="createTime" label="下单时间" />-->

      <el-table-column label="状态" width="100">

        <template #default="scope">
            <el-tag>{{ getBookStatusName(scope.row.status) }}</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="操作" >
        <template #default="scope">
          <el-button size="small" v-if="scope.row.status === 2" @click="handleConfirm('接单',scope.row.num)">接单</el-button>
          <el-button size="small" v-if="scope.row.status === 2" type="danger" @click="handleConfirm">拒单</el-button>
          <el-button size="small" v-if="scope.row.status === 3" type="primary" @click="handleConfirm('派送',scope.raw.num)">派送</el-button>
          <el-button size="small" v-if="scope.row.status === 4" type="success" @click="handleConfirm('完成',scope.raw.num)">完成</el-button>
        </template>
      </el-table-column>

    </el-table>

    <div style="margin-top: 10px;">
      <el-pagination
          :page-sizes="[10, 20, 30, 40]"
          :page-size="queryForm.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handlePageChange"
      />
    </div>
  </div>
</template>

<script setup>
import { reactive, ref} from "vue";
import { useRouter } from "vue-router";
// import {getBookList} from  "@/api/book"
import {getBookList,completeBook, deliveryBook, getBookDetail, receiveBook, rejectBook} from "@/api/book"
import {ElMessageBox,ElMessage} from "element-plus";

const router = useRouter();
const queryForm = reactive({
  status:null,
  num: null,
  phone: null,
  valueTime: "",
  page: 1,
  pageSize: 10
});

let bookList=reactive([]);
let total=ref(0);


let handleStatus=()=>{
  if(queryForm.status==0){
    queryForm.status = null;
  }
  loadBookList();
}

let handleConfirm=(operation,num)=>{

  //确认对话框
  ElMessageBox.confirm(
      `是否${operation}?`,
      '提示',
      {
        confirmButtonText: `${operation}`,
        cancelButtonText: '取消',
        type: 'warning',
      }
  ).then(() => {
    if("接单" == operation){

      receiveBook(num).then((resp)=>{
        if(resp.code == 200){
          ElMessage.success(resp.msg);
          loadBookDetail();
        }else{
          ElMessage.error(resp.msg);
        }
      }).catch((error)=>{

      })
    }else if("派送"==operation){
      deliveryBook(num).then((resp)=>{
        if(resp.code == 200){
          ElMessage.success(resp.msg);
          loadBookDetail();
        }else{
          ElMessage.error(resp.msg);
        }
      }).catch((error)=>{

      })
    }else if("完成"==operation){
      completeBook(num).then((resp)=>{
        if(resp.code == 200){
          ElMessage.success(resp.msg);
          loadBookDetail();
        }else{
          ElMessage.error(resp.msg);
        }
      }).catch((error)=>{

      })
    }
  }).catch(()=>{
  })
}


let getBookStatusName=(status)=>{
  if (status=== 1) {
    return '待付款'
  } else if (status === 2) {
    return '待接单'
  } else if (status === 3) {
    return '已接单'
  } else if (status === 4) {
    return '派送中'
  } else if (status === 5) {
    return '已完成'
  } else if (status === 6) {
    return '已取消'
  } else {
    return '退款'
  }
}

//定义方法
let loadBookList=()=>{

  if(queryForm.valueTime){

    if(queryForm.valueTime[0]){
      queryForm.beginTime = queryForm.valueTime[0];
    }

    if(queryForm.valueTime[1]){
      queryForm.endTime = queryForm.valueTime[1];
    }

  }else{
    queryForm.beginTime = "";
    queryForm.endTime = "";
  }

  let param = JSON.parse(JSON.stringify(queryForm));
  delete param.valueTime;
      //准备数据
  getBookList(param).then((resp)=>{
    bookList.length = 0;
    bookList.push(... resp.data);

    total.value=resp.total;
  });
}

let handleSizeChange=(pageSize)=>{
  queryForm.pageSize = pageSize;
  loadBookList()
}

let handlePageChange=(page)=>{
  queryForm.page = page;
  loadBookList();
}

let handleDetail=(num)=>{
  router.push("/book/"+num);
}

loadBookList();
</script>

<style scoped>
</style>
